﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanoAula.Core.Helper;
using PlanoAula.Repository;
using PlanoAula.BD;

namespace PlanoAula
{
    public partial class ListPlanoAulaForm : FormEdicao
    {
        IPlanoAulaCabRepository repositoryPlanoAula;
        public ListPlanoAulaForm() : this(new PlanoAulaCabRepository()) { }

        public ListPlanoAulaForm(IPlanoAulaCabRepository repositoryPlanoAula)
            : base(Guid.Empty)
        {
            this.repositoryPlanoAula = repositoryPlanoAula;
            InitializeComponent();
            CarregarGrid();
        }

        private void CarregarGrid()
        {
            planoAulaCabBindingSource.DataSource = from dados in repositoryPlanoAula.GetAll()
                                                   orderby dados.Ate descending, dados.Universidade, dados.Curso, dados.Materia
                                                   select dados;
        }
        
        private void toolStripButtonAdicionar_Click(object sender, EventArgs e)
        {
            new PlanoAulaCabForm().ExbirFormulario(this);
            CarregarGrid();
        }

        private void dgvItem_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (planoAulaCabBindingSource.Current != null)
            {
                var id = ((PlanoAulaCab)planoAulaCabBindingSource.Current).Id;
                new PlanoAulaCabForm(id).ExbirFormulario(this);
                CarregarGrid();
            }
        }

        private void toolStripButtonPlanoDeAula_Click(object sender, EventArgs e)
        {
            if (planoAulaCabBindingSource.Current != null)
            {
                var id = ((PlanoAulaCab)planoAulaCabBindingSource.Current).Id;
#warning reativar isso depois
                //new relPlanoAulaForm(id).ExbirFormulario(this);
                CarregarGrid();
            }
        }

        private void detalhesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvItem_MouseDoubleClick(sender, null);
        }

        private void HabilitarBotao(bool Habilitar)
        {
            excluirToolStripMenuItem.Enabled = Habilitar;
            detalhesToolStripMenuItem.Enabled = Habilitar;
            toolStripButtonPlanoDeAula.Enabled = Habilitar;
        }
        private void dgvItem_SelectionChanged(object sender, EventArgs e)
        {
            HabilitarBotao( planoAulaCabBindingSource.Current != null );
        }

        private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (planoAulaCabBindingSource.Current != null)
            {
                if (MessageBox.Show("Deseja excluir o item selecionado?", "Excluir", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    var id = ((PlanoAulaCab)planoAulaCabBindingSource.Current).Id;
#warning Verificar se está em cascata
                    repositoryPlanoAula.Delete(id);
                    MessageBox.Show("Item excluído com sucesso.");
                    CarregarGrid();
                }
            }
        }

        
    }
}
